﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Business.Abstract;
using DataAccess.Repository;
using DataAccess.NHibernateHelper;
using Domain.Entity;
using log4net;

namespace Business.Service
{
    public class CustomerServiceImpl : ICustomerService
    {
        ILog log = LogManager.GetLogger("CustomerServiceImpl");
        CustomerRepository customerRepo;

        public CustomerServiceImpl()
        {
            customerRepo = new CustomerRepository(FluentNHibernateHelper.GetContext());
        }

        public IList<Customer> GetAllCustomers()
        {
            return customerRepo.List().Where(x => x.IsDeleted == false).ToList();
        }

        public void SaveCustomer(Customer customer)
        {
            log.Debug("Save customer");
            customer.CreatedDate = DateTime.Now;
            customerRepo.Insert(customer);
            customerRepo.Commit();
        }

        public Customer GetCustomerById(long id)
        {
            return customerRepo.List().Where(x => x.IsDeleted == false).ToList().Where(x=>x.ID == id).First();
        }

        public void UpdateCustomer(Customer customer)
        {
            customerRepo.Update(customer);
            customerRepo.Commit();
        }

        public void DeleteCustomer(Customer customer)
        {
            customerRepo.Delete(customer);
            customerRepo.Commit();
        }

        public IList<Customer> GetAllCustomers(string column, string value)
        {
            return customerRepo.GetAllCustomers(column, value);
        }

        public Customer GetCustomerByEmail(string email)
        {
            return customerRepo.GetCustomerByEmail(email);
        }

    }
}
